package com.lehecai.core.lottery.fetcher.jcgyj.impl.worker;

import com.lehecai.core.exception.FetchFailedException;
import com.lehecai.core.exception.UnsupportedFetcherTypeException;
import com.lehecai.core.lottery.JcgjSPType;
import com.lehecai.core.lottery.LotteryType;
import com.lehecai.core.lottery.fetcher.FetcherType;
import com.lehecai.core.lottery.fetcher.jcgyj.AbstractJcgjStaticInstantSpFetchWorker;
import com.lehecai.core.lottery.fetcher.jcgyj.IJcgjStaticInstantSpFetcher;
import com.lehecai.core.lottery.fetcher.jcgyj.JcgjStaticInstantSpItem;
import com.lehecai.core.lottery.fetcher.jcgyj.impl.CommonJcgjStaticInstantSpFetcher;
import com.lehecai.core.util.CoreJSONUtils;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 竞彩足球猜冠军固定奖金即时sp抓取实现类
 * @author qatang
 *
 */
public class CommonJcgjStaticInstantSpFetchWorkerOfficial extends AbstractJcgjStaticInstantSpFetchWorker {
	private final Logger logger = LoggerFactory.getLogger(this.getClass().getName());
	
	public List<JcgjStaticInstantSpItem> fetch(String phase, LotteryType lotteryType) throws FetchFailedException {

        if (StringUtils.isEmpty(phase)) {
            throw new FetchFailedException("必须指定彩期");
        }

        JSONArray raceArray;
        try {
            raceArray = AbstractJcgjFetchWorkerOfficial.fetchAll(phase);
        } catch (Exception e) {
            throw new FetchFailedException("抓取竞彩猜冠军数据出错, phase=" + phase);
        }

        if (raceArray == null || raceArray.isEmpty()) {
            logger.warn("抓取竞彩猜冠军数据为空, phase={}", phase);
            return null;
        }

        List<JcgjStaticInstantSpItem> jcgjStaticInstantSpItems = new ArrayList<JcgjStaticInstantSpItem>();
        for (Object aRaceArray : raceArray) {
            JSONObject raceObject = (JSONObject) aRaceArray;

            JcgjStaticInstantSpItem item = new JcgjStaticInstantSpItem();
            item.setPhase(phase);
            item.setMatchNum(raceObject.getString("matchNum"));
            item.setTeam(raceObject.getString("team"));

            Map<JcgjSPType, String> map = new HashMap<JcgjSPType, String>();
            map.put(JcgjSPType.GJ, CoreJSONUtils.getString(raceObject, "sp"));

            item.setSpmap(map);

            jcgjStaticInstantSpItems.add(item);
        }

		return jcgjStaticInstantSpItems;
	}
	
	public static void main(String[] args) {
		IJcgjStaticInstantSpFetcher fetcher = new CommonJcgjStaticInstantSpFetcher();
		List<JcgjStaticInstantSpItem> jcgjStaticInstantSpItemList = null;
		try {
			jcgjStaticInstantSpItemList = fetcher.fetch("2014001", FetcherType.T_OFFICIAL, LotteryType.JCGJ);
		} catch (UnsupportedFetcherTypeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		for (JcgjStaticInstantSpItem item : jcgjStaticInstantSpItemList) {
			System.out.println(item.getSpmap());
		}
	}
}
